<?php
namespace app\admin\controller;

use app\AdminCollection;
use app\model\Admin;
use app\model\AdminGroup;
use app\model\Menu;
use My\Tool;
use My\Validate;

class Group extends AdminCollection
{
    public function __construct()
    {
        parent::__construct();
    }

    //权限列表
    public function index()
    {
        $list = AdminGroup::pageService();
        $_list = array();
        foreach ($list as $key => $val) {
            $_t = $val;
            $_t['statusString'] = $this->getConfig('status', $val['status']);
            $_t['updateUrl'] = url('group/update', array('admin_group_id' => $val['admin_group_id']));
            $_t['deleteUrl'] = url('group/delete', array('admin_group_id' => $val['admin_group_id']));
            $_list[] = $_t;
        }
        $pages = $list->render();
        $this->assign('list', $_list);
        $this->assign('pages', $pages);
        return $this->fetch();
    }

    //编辑权限
    public function update()
    {
        $submit = input('submit');
        if ($submit) {
            try {
                $input = input('');
                $where = $input['where'];
                $data = $input['info'];
                $rules = isset($input['rules']) ? $input['rules'] : '';
                if (Validate::checkNull($data['name'])) throw new \Exception("请输入权限名称");
                if (config('default_group_id') != $where['admin_group_id']) {
                    if (Validate::checkEmpty($rules)) throw new \Exception("请选择权限节点");
                    $data['rules'] = Tool::arrayString($rules);
                }
                AdminGroup::updateServer($where, $data);
                $this->returnSuccess("提交数据成功", $this->getPrevious());
            } catch (\Exception $e) {
                $this->returnError($e->getMessage());
            }
        } else {
            $admin_group_id = input('admin_group_id');
            if (Validate::checkNull($admin_group_id)) $this->returnError('系统发生致命错误');
            $findService = AdminGroup::findService(['admin_group_id' => $admin_group_id]);
            if (Validate::checkEmpty($findService)) $this->returnError('系统发生致命错误');
            $this->assign('findService', $findService);
            $groupList = Menu::menuGroupTree($findService['rules']);
            $this->assign('groupList', $groupList);
            $this->assign('admin_group_id', $admin_group_id);
            $this->assign('default_group_id', config('default_group_id'));
            $this->assign('REFERER', $this->previousUrl());
            return $this->fetch();
        }
    }

    //删除权限
    public function delete()
    {
        try {
            $admin_group_id = input('admin_group_id');
            if (Validate::checkNull($admin_group_id)) throw new \Exception('系统发生致命错误');
            $findService = AdminGroup::findService(['admin_group_id' => $admin_group_id]);
            if (Validate::checkEmpty($findService)) throw new \Exception('系统发生致命错误');
            if (config('default_group_id') == $admin_group_id) throw new \Exception('超级管理员权限组不能删除');
            $adminFindService = Admin::findService(['group_id' => $admin_group_id]);
            if ($adminFindService) throw new \Exception($findService['name'] . '：权限已经启动，暂时不能删除');
            $return = AdminGroup::deleteService(['admin_group_id' => $admin_group_id]);
            if ($return) {
                $this->returnSuccess("删除数据成功");
            } else {
                throw new \Exception("删除数据失败");
            }
        } catch (\Exception $e) {
            $this->returnError($e->getMessage());
        }
    }

    //添加权限
    public function add()
    {
        $submit = input('submit');
        if ($submit) {
            try {
                $input = input('');
                $data = $input['info'];
                $rules = isset($input['rules']) ? $input['rules'] : '';
                if (Validate::checkNull($data['name'])) throw new \Exception("请输入权限名称");
                if (Validate::checkEmpty($rules)) throw new \Exception("请选择权限节点");
                $data['rules'] = Tool::arrayString($rules);
                $return = AdminGroup::addService($data);
                if ($return) {
                    $this->returnSuccess("提交数据成功");
                } else {
                    throw new \Exception("提交数据失败");
                }
            } catch (\Exception $e) {
                $this->returnError($e->getMessage());
            }
        } else {
            $groupList = Menu::menuGroupTree();
            $this->assign('groupList', $groupList);
            return $this->fetch();
        }
    }
}
